package com.example.shiro.controller;

import com.example.shiro.po.Response;
import com.example.shiro.po.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author xyh
 * @description
 * @date 2021/4/1 16:13
 */
@RequestMapping("/login")
@RestController
public class LoginController {


    @RequestMapping("/login")
    public Response<User> login(String userName, String password){
        Response<User> response = new Response<>();
        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken();
            token.setUsername(userName);
            token.setPassword(password.toCharArray());
            subject.login(token);
            User user = (User) subject.getPrincipal();
            response.setCode("200");
            response.setData(user);
        }catch (Exception e){
            e.printStackTrace();
            response.setCode("500");
            response.setMessage(e.getMessage());
            return response;
        }


        return response;
    }
}
